package com.example.hospital.api.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;
import com.example.hospital.api.common.R;
import com.example.hospital.api.controller.form.InsertDoctorForm;
import com.example.hospital.api.controller.form.InsertDoctorPrescriptionForm;
import com.example.hospital.api.controller.form.SearchPrescriptionByIdForm;
import com.example.hospital.api.controller.form.SearchPrescriptionByRegistrationIdForm;
import com.example.hospital.api.db.dao.DoctorPrescriptionDao;
import com.example.hospital.api.service.DoctorPrescriptionService;
import com.example.hospital.api.service.DrugNumService;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/prescription")
public class DoctorPrescriptionController {
    @Resource
    private DoctorPrescriptionService doctorPrescriptionService;

    @Resource
    private DrugNumService drugNumService;

    @Resource
    private DoctorPrescriptionDao doctorPrescriptionDao;

    @PostMapping("/searchPrescriptionById")
    @SaCheckLogin
    public R searchPrescriptionById(@RequestBody @Valid SearchPrescriptionByIdForm form) {
        HashMap map = doctorPrescriptionService.searchPrescriptionById(form.getId());
        return R.ok(map);
    }

    @PostMapping("/insert")
    @SaCheckLogin
    public R insert(@RequestBody @Valid InsertDoctorPrescriptionForm form) {
        Map param=BeanUtil.beanToMap(form);
        doctorPrescriptionService.contextLoads(param);
        doctorPrescriptionDao.insertOut(form.getId());
        return R.ok();
    }

    @PostMapping("/searchCount")
    @SaCheckLogin
    public R searchCount(@RequestBody @Valid SearchCountForm form) {
        int result=doctorPrescriptionService.searchCount(form.getId());
        return R.ok().put("result",result);
    }

    @PostMapping("/outDrug")
    @SaCheckLogin
    public R outDrug(@RequestBody @Valid InsertDoctorPrescriptionForm form) {
        drugNumService.reduce(form.getId());
        doctorPrescriptionDao.upsertOut(form.getId());
        return R.ok();
    }

    @PostMapping("/searchPrescriptionByRegistrationId")
    @SaCheckLogin
    public R searchPrescriptionByRegistrationId(@RequestBody @Valid SearchPrescriptionByRegistrationIdForm form) {
        Map param = BeanUtil.beanToMap(form);
        HashMap map = doctorPrescriptionService.searchPrescriptionByRegistrationId(param);
        return R.ok().put("result", map);
    }
}

